package com.intellagric.service.agritech.impl;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.dao.agritech.AgtResourceDao;
import com.intellagric.mapper.AgtResourceMapper;
import com.intellagric.mapper.AgtUserLikeMapper;
import com.intellagric.pojo.AgtResource;
import com.intellagric.pojo.AgtUserLike;
import com.intellagric.pojo.AgtUserLikeExample;
import com.intellagric.service.agritech.UserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/28 19:04
 * @Description:
 */
@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private AgtUserLikeMapper agtUserLikeMapper;

//    @Autowired
//    private AgtUserLikeDao agtUserLikeDao;

    @Autowired
    private AgtResourceMapper agtResourceMapper;

    @Autowired
    private AgtResourceDao agtResourceDao;

    @Override
    public ResponseMessage addUserLike(AgtUserLike userLike) {
        AgtUserLikeExample agtUserLikeExample = new AgtUserLikeExample();
        agtUserLikeExample.createCriteria().andIdIsNotNull().andUserIdEqualTo(userLike.getUserId()).andResourceIdEqualTo(userLike.getResourceId());
        if (agtUserLikeMapper.selectByExample(agtUserLikeExample).size() != 0) {
            return this.cancelUserLike(userLike);
        }
        Date createdTime = new Date();
        String userLikeId = UUIDUtils.getID();

        userLike.setId(userLikeId);
        userLike.setCreatedTime(createdTime);

        return agtResourceDao.increaseResourceLikeCount(userLike.getResourceId(), 1) == 1 ? (
                agtUserLikeMapper.insertSelective(userLike) == 1 ? ResponseMessage.success().add("add", userLike) : ResponseMessage.fail()
        ) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage cancelUserLike(AgtUserLike userLike) {

        userLike.setIsCanceled("1");

        return agtResourceDao.increaseResourceLikeCount(userLike.getResourceId(), -1) == 1 ? (
                agtUserLikeMapper.updateByPrimaryKeySelective(userLike) == 1 ? ResponseMessage.success() : ResponseMessage.fail()
        ) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteUserLike(String id) {
        return agtUserLikeMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editUserLike(AgtUserLike userLike) {
        return agtUserLikeMapper.updateByPrimaryKeySelective(userLike) == 1 ? ResponseMessage.success().add("update", userLike) : ResponseMessage.fail();
    }

    @Override
    public AgtUserLike getUserLikeById(String id) {
        return agtUserLikeMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getUserLikeList() {
        AgtUserLikeExample agtUserLikeExample = new AgtUserLikeExample();
        agtUserLikeExample.setOrderByClause("created_time ASC");
        agtUserLikeExample.createCriteria().andIdIsNotNull();
        List<AgtUserLike> agtUserLikeList = agtUserLikeMapper.selectByExample(agtUserLikeExample);
        return LayuiDataGridResult.success().add(agtUserLikeList, agtUserLikeList.size());
    }

    @Override
    public LayuiDataGridResult getUserLikeByPage(int page, int limit) {
        PageHelper.startPage(page, limit);
        AgtUserLikeExample userLikeExample = new AgtUserLikeExample();
        userLikeExample.createCriteria().andIdIsNotNull();
        List<AgtUserLike> userLikeList = agtUserLikeMapper.selectByExample(userLikeExample);
        return LayuiDataGridResult.success().add(userLikeList, this.getUserLikeList().getCount());
    }

/*    @Override
    public LayuiDataGridResult getUserLikeListByVo(AgtUserLikeVo agtUserLikeVo, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<AgtUserLike> userLikeList = agtUserLikeDao.selectUserLikeListByVo(agtUserLikeVo);
        return LayuiDataGridResult.success().add(userLikeList, userLikeList.size());
    }*/
}
